import threading


class Client(threading.Thread):
    def __init__(self, clientConnection, name="client"):
        super().__init__(name=name)
        self.clientConnection = clientConnection
        self.image = None
        self.lock = threading.RLock()

    def registerImage(self, image):
        self.image = image

    def sendHeader(self):
        self.clientConnection.send("HTTP/1.1 200 OK\r\n".encode())
        self.clientConnection.send("Content-Type: multipart/x-mixed-replace; boundary=y5exa7CYPPqoASFONZJMz4Ky\r\n".encode())
        self.clientConnection.send("Cache-Control: no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0\r\n".encode())
        self.clientConnection.send("Pragma: no-cache\r\n".encode())
        self.clientConnection.send("Connection: keep-alive\r\n".encode())
        self.clientConnection.send("\r\n".encode())

    def run(self):
        # self.lock.acquire()
        self.clientConnection.send("--y5exa7CYPPqoASFONZJMz4Ky\r\n".encode())
        self.clientConnection.send("Content-Type: image/jpeg\r\n".encode())
        self.clientConnection.send(f"Content-Length: {len(self.image)}\r\n".encode())
        self.clientConnection.send("\r\n".encode())
        self.clientConnection.send(self.image)
        self.clientConnection.send("\r\n".encode())
        # self.lock.release()


